import Vue from 'vue'
export default {
  namespaced: true,
  state: {
    // 系统信息
    system:{},
    modules:[],
    isFirstLoad:true,
    isCollapse: false,
    isFullScren: false,
    screen: -1,
    screenHeight:-1,
    
    pageAuth:{},

    request:[], // 用来保存最近20次请求参数和请求地址

    axiosLoadingNum:0
  },
  actions: {
    init({ commit }, { system } ) {
      commit('init', system)
    },

    setPageAuth({ commit },data){
      commit('setPageAuth', data)
    },
    addRequest({ commit },data){
      commit('SET_REQUEST', data)
    },

    setAxiosLoadingNum({ commit },num){
      //console.log(num,'setAxiosLoadingNum');
      commit('SET_AXIOS_LOADING_NUM', num)
      
    }
  },
  mutations: {
    init:(state,system)=>{
      system.webconfig.language.type = window.$getStore({ name: 'language' }) || system.webconfig.language.type
      system.webconfig.theme.nav = window.$getStore({ name: 'theme-nav' }) || system.webconfig.theme.nav
      system.webconfig.theme.colorType = window.$getStore({ name: 'theme-colorType' }) || system.webconfig.theme.colorType
      system.webconfig.theme.color = window.$getStore({ name: 'theme-color' }) || system.webconfig.theme.color
      system.webconfig.theme.sideWidth = window.$getStore({ name: 'theme-sideWidth' }) || system.webconfig.theme.sideWidth
      system.webconfig.theme.showTabs = window.$getStore({ name: 'theme-showTabs' }) || system.webconfig.theme.showTabs
      system.webconfig.theme.showIcon = window.$getStore({ name: 'theme-showIcon' }) || system.webconfig.theme.showIcon

      state.system = system.webconfig
      state.modules = system.modules
    },
    setPageAuth:(state,data)=>{
      Vue.set(state.pageAuth,data.moduleCode,data)
    },
    SET_LANGUAGE: (state, language) => {
      state.system.language.type = language
      window.$setStore({
        name: 'language',
        content: language
      })
    },
   

    SET_COLLAPSE: (state) => {
      state.isCollapse = !state.isCollapse;
    },
    SET_FULLSCREN: (state) => {
      state.isFullScren = !state.isFullScren;
    },
    SET_SCREEN: (state,screen) => {
      state.screen = screen;
    },
    SET_SCREEN_HEIGHT: (state,screenHeight) => {
      state.screenHeight = screenHeight;
    },
    SET_ISFIRSTLOAD: (state,isFirstLoad) => {
      state.isFirstLoad = isFirstLoad;
    },

    SET_REQUEST:(state,data)=>{
      if(state.request.length == 20){
        state.request.splice(0,1)
      }
      state.request.push(data)
    },
    SET_AXIOS_LOADING_NUM:(state,data)=>{
      state.axiosLoadingNum += data
    },

    // 主题设置
    SET_THEME_NAV: (state, themeNav) => {
      state.system.theme.nav = themeNav
      window.$setStore({
        name: 'theme-nav',
        content: themeNav
      })
    },
    SET_THEME_COLORTYPE: (state, themeColorType) => {
      state.system.theme.colorType = themeColorType
      window.$setStore({
        name: 'theme-colorType',
        content: themeColorType
      })
    },
    SET_THEME_COLOR: (state, themeColor) => {
      state.system.theme.color = themeColor
      window.$setStore({
        name: 'theme-color',
        content: themeColor
      })
    },
    SET_THEME_SIDEWIDTH: (state, sideWidth) => {
      state.system.theme.sideWidth = sideWidth
      window.$setStore({
        name: 'theme-sideWidth',
        content: sideWidth
      })
    },
    SET_THEME_HEADERHEIGHT: (state, headerHeight) => {
      state.system.theme.headerHeight = headerHeight
      window.$setStore({
        name: 'theme-headerHeight',
        content: headerHeight
      })
    },
    SET_THEME_SHOWTABS: (state, showTabs) => {
      state.system.theme.showTabs = showTabs
      window.$setStore({
        name: 'theme-showTabs',
        content: showTabs
      })
    },
    SET_THEME_SHOWICON: (state, showIcon) => {
      state.system.theme.showIcon = showIcon
      window.$setStore({
        name: 'theme-showIcon',
        content: showIcon
      })
    }
  }
}